package com.cheyun.netsalev3.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.bean.KeyValueParam


/**
 * 二级列表适配器.
 */

class SecondAdpetr(var topLayoutId: Int, var itemLayoutId: Int, var viewModel: Any? = null) : SecondaryListAdapter<SecondAdpetr.ViewHolder, SecondAdpetr.ViewHolder>() {
    override fun onGroupItemBindViewHolder(data: Any?, viewHolder: ViewHolder, groupItemIndex1: Int) {
        viewHolder.apply {
            bind(data!!, viewModel,groupItemIndex1)
            itemView.tag = data
        }
    }

    override fun onSubItemBindViewHolder(data: Any?, viewHolder: ViewHolder, groupItemIndex: Int, subItemIndex: Int) {
        viewHolder.apply {
            bind(data!!, viewModel,groupItemIndex,subItemIndex)
            itemView.tag = data
        }
    }


    private var dts: List<SecondaryListAdapter.DataTree<String, KeyValueParam>> = ArrayList()
    fun setData(datas: List<SecondaryListAdapter.DataTree<String, KeyValueParam>>) {
        dts = datas
        notifyNewData(dts)
        setDefaultShow(0)
    }

    override fun groupItemViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), topLayoutId, parent, false))
    }


    override fun subItemViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), itemLayoutId, parent, false))
    }


//    override fun onSubItemBindViewHolder(holder: RecyclerView.ViewHolder, groupItemIndex: Int, subItemIndex: Int) {
//
//        (holder as SubItemViewHolder).apply {
//            binding.lineView.visibility = if (subItemIndex == 0) View.GONE else View.VISIBLE
//            if (subItemIndex % 2 == 0) {
//                binding.layoutBg.setBackgroundResource(R.color.white)
//            } else {
//                binding.layoutBg.setBackgroundResource(R.color.club_layout_bg)
//            }
//
//        }
//
//    }

    //    private var rotateAnimation1: Animation? = null
//    private var rotateAnimation2: Animation? = null
    override fun onGroupItemClick(isExpand: Boolean, holder: ViewHolder, groupItemIndex: Int) {
//        if (rotateAnimation1 == null) {
//            rotateAnimation1 = AnimationUtils.loadAnimation(context, R.anim.rotate_anim)
//        }
//        if (rotateAnimation2 == null) {
//            rotateAnimation2 = AnimationUtils.loadAnimation(context, R.anim.rotate_anim_2)
//        }
//        holder.apply {
//            binding.imageView.clearAnimation()
//            if (isExpand) {
//                binding.bottomView.visibility = View.GONE
//                binding.imageView.startAnimation(rotateAnimation2)
//            } else {
//                binding.bottomView.visibility = View.VISIBLE
//                binding.imageView.startAnimation(rotateAnimation1)
//            }
//        }
//        Toast.makeText(context, "group item " + groupItemIndex.toString() + " is expand " +
//                isExpand.toString(), Toast.LENGTH_SHORT).show()

    }

    override fun onSubItemClick(holder: ViewHolder, groupItemIndex: Int, subItemIndex: Int) {

//        Toast.makeText(context, "sub item " + subItemIndex.toString() + " in group item " +
//                groupItemIndex.toString(), Toast.LENGTH_SHORT).show()

    }

    class ViewHolder(var binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Any, viewModel: Any?,groupItemIndex:Int, pos: Int = adapterPosition) {
            binding.apply {
                setVariable(BR.item, item)
                setVariable(BR.viewModel, viewModel)
                setVariable(BR.binding, this)
                setVariable(BR.position, pos)
                setVariable(BR.groupItemIndex, groupItemIndex)
                executePendingBindings()
            }
        }

    }


}